// JavaSc ript Document
$(function(){

//全局变量
var windowH , windowW , scrolltop , id;
windowW = $(window).width();
windowH = $(window).height();

//全局变量

//切换
function tabs( obj )
{
	var tabsChildren = null;
	if( $(obj) )
	{
		tabsChildren = $(obj).children();
		if ($('#tabs li a').eq(0).html() == '综合') {
			tabsChildren.show();
		} else{
			tabsChildren.eq(0).show();
		}
		tabsChildren.eq(0).show();
		$('#tabs li').each(function(index){
			$(this).click(function(){

				if ($(this).children().html() == '综合') {
					$(this).addClass('active').siblings().removeClass('active')
					tabsChildren.show();
				} else{
					$(this).addClass('active').siblings().removeClass('active')
					tabsChildren.eq(index).show().siblings().hide();
				}

			});
		});
	};
};
tabs('.tabs')
//切换


//控制首页的table

if( $('.table') )
{
	$('.table td img').each(function(){
		$(this).css({'width':''+$(this).parent().width()+'px' , 'height':''+$(this).parent().height()+'px'})
	});
}

//控制首页的table

//首页fix 效果
$('.fix a').mouseover(function(){
	$(this).addClass('active').siblings().removeClass('active');
});
//首页hover效果

//回到顶部
returnTop()
$(window).scroll(function(){
	scrolltop = $(window).scrollTop();
	returnTop();
})
function returnTop()
{
	if( scrolltop > windowH)
	{
		$('.top').fadeIn(1000)
	}else{
		$('.top').fadeOut(1000)
	};
	$('.top').click(function(){
		$(window).scrollTop(0);
	});
};
//回到顶部

//更多菜单
$('#moll').click(function(){
	$("#afterFix").slideToggle();
	$("#fanhui").toggle();
	$("#gengduo").toggle();
	$("#moll").removeClass("active")
	// var x =  $('#afterFix').width();
	// event.stopPropagation();
	// $('#afterFix').animate({
	// 	right : 0 + 'px'
	// });
});

$('#moll').mouseover(function(){
	$("#moll").addClass("active")
});
$('#moll').mouseleave(function(){
	$("#moll").removeClass("active")
});

$('#h').mouseover(function(){
	$("#h").addClass("active")
});
$('#h').mouseleave(function(){
	$("#h").removeClass("active")
});

$('#afterFix a').click(function(){
	$(this).addClass("active");
	$(this).siblings().removeClass("active");
});

//更多菜单

//首页菜单
function indexEvent()
{
	var menuW = null;
	menuW = $('.menuList').width()+20;
	$('.menuList').css('right',-menuW + 'px');
	$('.indexList').css('left',windowW + 'px')
	$('#menu').click(function(ev){
		// $('.menuList').show().animate({ right : '0px' });
		event.stopPropagation();
	});
	// $(document).click(function(){
	// 	$('.menuList').animate({ right :-menuW +'px' },function(){
	// 		$('.menuList').hide();
	// 	});
	// 	$('#afterFix').animate({
	// 		right : '-100%'
	// 	});
	// });
// 	//解决苹果兼容
// 	$("body").children().click(function () {});
	//解决苹果兼容

/*
*change begin
*/
/*****************************************88*/
$('.menu').on('click',function(){
	$('.indexList').show().animate({left:'0px'})
})

/*****************************************88*/

	$('.menuList div').each(function(index){
		if( $(this).index() > 1 )
		{
			$(this).click(function(){
				$('.indexList').show().animate({left : '0px'})
			});
		};
	});
	/*****************************************/

	$('.retRun').click(function(){
		$('.indexList').animate({ left :windowW +'px' },function(){
			$('.indexList').hide();
		});
		event.stopPropagation();
	});
	/*****************************************/

	$('.indexList .retRun').click(function(ev){
		$('.indexList').animate({ left :windowW +'px' },function(){
			$('.indexList').hide();
		});
		$('.menuList').show().animate({ right : '0px' });
		event.stopPropagation();
	});
};
/*
*change end
*/
indexEvent();

//首页菜单

//food菜单
function tart( obj )
{
	var childH2 = obj.children().children('h2');
	var childUl = obj.children().children('ul');
	var childUlH = childUl.outerHeight();
	childUl.css({'height':'0px' , 'padding':'0px'});
	childUl.eq(0).css({'height':''+childUlH+'px' , 'padding':'10px'})
	childH2.click(function(){
		if( $(this).children().children('em').hasClass('angleTop') )
		{
			$(this).children().children('em').removeClass('angleTop').addClass('angleBottm');
			$(this).siblings().animate({ height:'0px' , padding:'0px'},200);
		}else{
			$(this).children().children('em').removeClass('angleBottm').addClass('angleTop');
			$(this).siblings().animate({ height:''+childUlH+'px' , padding:'10px'},200);
		};
	});
};
tart($('.food'));
//food菜单
function touch( obj ,id )
{
	var oDiv = document.getElementById(obj);
	var oUl = document.getElementById(id);
	var oBar = document.getElementById('bar');
	var downY = 0;
	var downTop = 0;
	var prevY = 0;
	var iSpeedY = 0;
	var timer = null;
	if( oDiv){
		oDiv.style.height = windowH - 43 + 'px';
		var barHeight = oDiv.offsetHeight * oDiv.offsetHeight / oUl.offsetHeight;
		if(barHeight <= 40){
			barHeight = 40;
		}
		else if( barHeight >= oDiv.offsetHeight ){
			barHeight = 0;
		}
	}
	//oBar.style.height = barHeight + 'px';

	oUl.ontouchstart = function(ev){
		var touchs = ev.changedTouches[0];
		downY = touchs.pageY;
		downTop = this.offsetTop;
		prevY = touchs.pageY;
		var bBtn = true;

		oUl.ontouchmove = function(ev){

			var touchs = ev.changedTouches[0];

			iSpeedY = touchs.pageY - prevY;
			prevY = touchs.pageY;
			oBar.style.opacity = 1;
			oBar.style.display = 'block';

			if(this.offsetTop >= 0){
				if(bBtn){
					bBtn = false;
					downY = touchs.pageY;
				}
				this.style.top = (touchs.pageY - downY)/3 + 'px';

				oBar.style.height = barHeight * ( 1 - this.offsetTop/oDiv.offsetHeight ) + 'px';
				oBar.style.top = 0;

			}
			else if(this.offsetTop <= oDiv.offsetHeight - oUl.offsetHeight){

				if(bBtn){
					bBtn = false;
					downY = touchs.pageY;
				}

				this.style.top = (touchs.pageY - downY)/3 + (oDiv.offsetHeight - oUl.offsetHeight) + 'px';

				oBar.style.height = barHeight * ( 1 - Math.abs((this.offsetTop - (oDiv.offsetHeight - oUl.offsetHeight)))/oDiv.offsetHeight ) + 'px';
				oBar.style.top = oDiv.offsetHeight - oBar.offsetHeight + 'px';

			}
			else{
				this.style.top = touchs.pageY - downY + downTop + 'px';
				var scaleY = this.offsetTop / (oDiv.offsetHeight - oUl.offsetHeight);
				//document.title = scaleY;
				oBar.style.top = scaleY * (oDiv.offsetHeight - oBar.offsetHeight) + 'px';
			}
			return false;
		};
		oUl.ontouchend = function(){
			this.ontouchmove = null;
			this.ontouchend = null;
			var This = this;
			//iSpeedY

			clearInterval(timer);
			timer = setInterval(function(){

				if( Math.abs(iSpeedY)<=100 || This.offsetTop > 50 || This.offsetTop <= oDiv.offsetHeight - oUl.offsetHeight - 50 ){

					clearInterval(timer);
					if( This.offsetTop >= 0 ){
						startMove(This,{top : 0},400,'easeOut',function(){

							startMove(oBar,{opacity : 0},400,'easeOut',function(){
								this.style.display = 'none';
							});

						});
						startMove(oBar,{height : barHeight},400,'easeOut');
					}
					else if( This.offsetTop <= oDiv.offsetHeight - oUl.offsetHeight ){
						startMove(This,{top : oDiv.offsetHeight - oUl.offsetHeight},400,'easeOut',function(){
							startMove(oBar,{opacity : 0},400,'easeOut',function(){
								this.style.display = 'none';
							});
					});
						startMove(oBar,{height : barHeight , top : oDiv.offsetHeight - barHeight},400,'easeOut');
					}
					else{
						startMove(oBar,{opacity : 0},400,'easeOut',function(){
							this.style.display = 'none';
						});
					}
				}
				else{
					iSpeedY *= 0.95;
					This.style.top = This.offsetTop + iSpeedY + 'px';


					if(This.offsetTop >= 0){
						oBar.style.height = barHeight * ( 1 - This.offsetTop/oDiv.offsetHeight ) + 'px';
						oBar.style.top = 0;
					}
					else if(This.offsetTop <= oDiv.offsetHeight - oUl.offsetHeight){
						oBar.style.height = barHeight * ( 1 - Math.abs((This.offsetTop - (oDiv.offsetHeight - oUl.offsetHeight)))/oDiv.offsetHeight ) + 'px';
						oBar.style.top = oDiv.offsetHeight - oBar.offsetHeight + 'px';
					}
					else{
						var scaleY = This.offsetTop / (oDiv.offsetHeight - oUl.offsetHeight);

						oBar.style.top = scaleY * (oDiv.offsetHeight - oBar.offsetHeight) + 'px';
					}

				}

			},13);
		};

	};
};
touch( 'touch' , 'ul1')
touch( 'touch' , 'ulLeft')


var a = $('#touch').height();
$('#ulLeft .minListLeft ul li').each(function(index){

		$(this).on('click' , function(){

			if( index != $('#ulLeft .minListLeft ul li').length -1 )
			{
				$('#ul1').animate({top:-$('#ul1>div').eq(index).position().top+'px'},300);
				$(this).addClass('active').siblings().removeClass('active');
			}else
			{
				$('#ul1').animate({top:-$('#ul1>div').eq(index).position().top + (a - $('#ul1>div').eq(index).height())+'px'},300);
				$(this).addClass('active').siblings().removeClass('active');
			};
		});
});

//$(selector).bind(event,data,function)





});